//
//  HMVistorViewController.swift
//  01-启动图片的搭建
//
//  Created by 白利兵 on 15/10/14.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class HMVistorViewController: UITableViewController {
    // 定义一个变量使外界可以访问该视图(主要是子控制器来访问)
    var vistorView: HMVistorView?
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
    // 重写父类的loadView
    override func loadView() {
        // 判断用户是否登录(这里要使用单例创建模型对象，否则会多次创建模型)
        let logon = UserAccountViewModel.shareUserAccount.logon
        if logon {
            super.loadView()
            return
        }
        // 进入访客视图
        setView()
    }
    
    private func setView() {
        // 添加按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "userRegisterClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Plain, target: self, action: "userLoginClick")
        // 替换根视图
        vistorView = HMVistorView()
        vistorView?.registerButton.addTarget(self, action: "userRegisterClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        vistorView?.loginButton.addTarget(self, action: "userLoginClick", forControlEvents: UIControlEvents.TouchUpInside)
        view = vistorView
    }
    
}
extension HMVistorViewController {
    // 监听注册按钮点击
    func userRegisterClick() {
        print("注册")
    }
    // 监听登录按钮的点击
    func userLoginClick() {
        print("登录")
        // 控制器跳转
        
       let vc = HMLoginViewController()
        let loginVC = HMLoginNavgController(rootViewController:vc)
        presentViewController(loginVC, animated: true, completion: nil)
    }
}









